/**
 * 
 */
package com.yerba.ricecooker.datasource;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;

import com.yerba.ricecooker.quote.Dividend;
import com.yerba.ricecooker.quote.Quote;
import com.yerba.ricecooker.utility.CommonLogger;

/**
 * @author mxie
 * 
 */
public class CSVLoader extends BaseLoader
{
    CommonLogger logger = CommonLogger.instance();

    public void load(String symbol, String quotefile, String divfile)
    {
        // loading dividend adjusted quotes
        getQuotes().clear();
        getQuotes().setSymbol(symbol);

        try
        {
            BufferedReader br = new BufferedReader(new FileReader(quotefile));
            String previousLine = "--Yahoo-CSV-Start--";

            String strLine = "";
            StringTokenizer st = null;

            // bypass the title line
            br.readLine();

            while ((strLine = br.readLine()) != null)
            {
                Quote quote = new Quote();
                st = new StringTokenizer(strLine, ",");

                // date
                if (st.hasMoreTokens())
                    quote.setDate(st.nextToken().trim());
                else
                    throw new Exception(previousLine);
                // open
                if (st.hasMoreTokens())
                    quote.setOpen(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                // high
                if (st.hasMoreTokens())
                    quote.setHigh(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                // low
                if (st.hasMoreTokens())
                    quote.setLow(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                // close
                if (st.hasMoreTokens())
                    quote.setClose(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                quote.setTrueClose(quote.getClose());
                // volume
                if (st.hasMoreTokens())
                    quote.setVolume(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                // adjclose
                if (st.hasMoreTokens())
                    quote.setAdjclose(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);

                getQuotes().add(quote);
            }

            // loading dividends
            br = new BufferedReader(new FileReader(divfile));
            previousLine = "--Yahoo-CSV-Dividend-Start--";

            strLine = "";
            st = null;

            // bypass the title line
            br.readLine();

            while ((strLine = br.readLine()) != null)
            {
                Dividend dividend = new Dividend();
                st = new StringTokenizer(strLine, ",");

                // date
                if (st.hasMoreTokens())
                    dividend.setDate(st.nextToken().trim());
                else
                    throw new Exception(previousLine);
                // dividend
                if (st.hasMoreTokens())
                    dividend.setDividend(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);

                getQuotes().getDividends().insert(dividend.getDate(), dividend);
            }

            // calculate true close values
            getQuotes().calculateTrueClose();
        }
        catch (Exception e)
        {
            logger.log(e.toString());
        }
    }
}
